/*
Note: This only works for state-by-year panel (not individual data)
*/



cap program drop createstack
program define createstack
args prominent smallcut smallcont //prominent = size of increase smallcut = smaller mw to cut, smallcont = smaller mw to control

//Merge in federal mw 
preserve
use "${data2}mw_state_annual.dta" , clear
ren statefips state_fips
replace year = year+1
tempfile fedmw
save `fedmw'
restore

merge 1:1 state_fips year using `fedmw', keep(3) nogen keepusing(mean_fed_mw)
gen byte fed_mw = round(mean_fed_mw,.0001)==round(mw,.0001) 

sort state_fips year
xtset state_fips year

cap drop mw_change
gen mw_change = d1.mw
replace mw_change=0 if mw_change==.


gen tryear_eff = year if mw_change>=`prominent'/100

sort tryear_eff state_fips 
gen eventid=_n if tryear_eff!=.

qui sum eventid 
local max = r(max)


/*========= TAKE 1: =========*/

local eventno = ""

qui{
if `max'!=. {
foreach i of numlist 1/`max' {
	preserve
	
		** Figure out Treatment State + Year
	sum tryear if eventid==`i'
	global tryear = r(mean)
				
	sum state_fips if eventid==`i'
	global trfips = r(mean)
	
	
	***Sample Cuts
				
	//Keep donor states w/o "treatment" 
	local lead = $lead
	local lag = $lag
	keep if inrange(year, $tryear -`lead', $tryear + `lag') 
	drop if mw_change >=`smallcut' & state_fips!=$trfips
				
	//Balanced Panel -- Treatment State should be balanced. 
	bys state_fips: gen count=_N
	qui sum count if state_fips==$trfips
	local numobs= r(mean)
	
	keep if count==`numobs'
	
	//Create smaller mw control
	gen small_inc = year if mw_change>=`smallcont'/100
	bys state_fips: egen small_year = min(small_inc) 
	gen byte mw_small = year>=small_year & state_fips!=$trfips
	
	//Keep only cases where there are control groups
	sum state_fips if state_fips!=$trfips
	local N =r(N) 
	if `N' >0  {
		sort state_fips year
		replace mw_change=0
		replace mw_change=1 if state_fips==$trfips & tryear==$tryear
		gen treat = state_fips==$trfips & year >= $tryear
		forval f = $lead(-1)1 {
			gen F`f'_D = F`f'.mw_change
			label var  F`f'_D "-`f'"
			replace F`f'_D = 0 if F`f'==.
			}
		forval l = 0/$lag  {
			gen L`l'_D = L`l'.mw_change
			label var  L`l'_D "`l'"
			replace L`l'_D = 0 if L`l'==.
			
			if `l'>0 {
				gen treatL`l'_D = L`l'.mw_change
				replace treatL`l'_D = 0 if treatL`l'==.
			}
		}
		
		gen event=`i'
		replace tryear=. if  state_fips!=$trfips
		tempfile event`i'
		save `event`i'', replace
		local eventno = "`eventno'" + "`i' " 
	}
	else {
	
	}
	restore
}

clear
foreach i of local eventno {
	append using `event`i''
}
}
}

replace year = year -1
merge m:1 state_fips year using "${data2}/aca_expansion_eitc.dta", keep(1 3) nogen


end


cap program drop plotes
program define plotes

  local range yla(-0.04(0.01)0.04)
  		coefplot ///
  			(D, omitted keep(F*D L*D )  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' legend(off) nooffsets ///
  			ytitle("Estimated Effect of Prominent Minimum Wage") xtitle("Years Since Prominent Minimum Wage Increase")


end

global lead=3
global lag=3

local rhs ur_high_edu wage_high_edu hpi eitc tanf4 snap4 ACA mw_small
local rhs2 eitc_dube gdppc ur mw_small


local inc = 25

// Dube Period
foreach inc of numlist 50 75 100 {
foreach div of numlist 1/9 {
	use ${data1}state_panel_mw_poverty_84, clear
	keep if year<2014
	keep if division==`div'

	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else  {
	createstack `inc' 25 10
	}
	
	cap gen eventid=.
	qui sum eventid 
	local max = r(max)
	
	if `max'!=. {

		replace F1_D=0
		reghdfe under_100 treat `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
		est save ${est}ster/other/prominent_stacked_es_`inc'_`div'a, replace
		
		reghdfe under_100 treat* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
		est save ${est}ster/other/prominent_stacked_es_`inc'_`div'a2, replace
		
		reghdfe under_100 treat `rhs2' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
		est save ${est}ster/other/prominent_stacked_es_`inc'_`div'b, replace
		
		reghdfe under_100 treat* `rhs2' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
		est save ${est}ster/other/prominent_stacked_es_`inc'_`div'b2, replace
	}

}


local div = "all"
use ${data1}state_panel_mw_poverty_84, clear
keep if year<2014

if `inc'>50 {
	createstack `inc' 50 10
} 
else  {
createstack `inc' 25 10
}

replace F1_D=0
reghdfe under_100 treat `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
est save ${est}ster/other/prominent_stacked_es_`inc'_`div'a, replace

reghdfe under_100 treat* `rhs' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
est save ${est}ster/other/prominent_stacked_es_`inc'_`div'a2, replace

reghdfe under_100 treat `rhs2' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
est save ${est}ster/other/prominent_stacked_es_`inc'_`div'b, replace

reghdfe under_100 treat* `rhs2' [pw=asecwt], absorb(state_fips event year) cl(state_fips)
est save ${est}ster/other/prominent_stacked_es_`inc'_`div'b2, replace

 

}


/*============== CREATE TABLES ================*/

local inc = 100

cap program drop mydisp
program mydisp, eclass
         matrix betass=e(b)
         matrix colnames betass = "E"
         mat list betass
         ereturn repost b = betass, rename
 end


local append outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau  keep(lnMW ) nolegend
local append1 outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau   nolegend


foreach inc of numlist 50 75 100 {
if `inc'==50 local divisions "1 2 3 4 5 6 7 8 9"
if `inc'==75 local divisions "1 2 3 4 5 7 8 9"
if `inc'==100 local divisions "1 2 3 4 5 8 9"

use ${data1}state_panel_mw_poverty_84, clear
keep if year<2014

if `inc'>50 {
	createstack `inc' 50 10
} 
else  {
createstack `inc' 25 10
}

qui sum N
local n = r(N) * r(mean)

foreach i of numlist 1/9 {
qui sum N if division==`i'
local n`i' = r(N) * r(mean)
}

********************** Our Cont *********************8


outreg, clear
/*Short Run*/

estimates use ${est}ster/other/prominent_stacked_es_`inc'_alla
lincomest treat
mydisp
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend ct("","(`div')"   )

foreach div of local divisions {
estimates use ${est}ster/other/prominent_stacked_es_`inc'_`div'a
lincomest treat
mydisp
`append1'  ct("","(`div')"   )
}
outreg, replay append(r)


/*Long Run*/
estimates use ${est}ster/other/prominent_stacked_es_`inc'_alla2
local obs : display %12.0gc `n'
lincomest treat + treatL1_D  + treatL2_D  + treatL3_D 
mydisp
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend  addrow("","`obs'")   

foreach div of local divisions {
estimates use ${est}ster/other/prominent_stacked_es_`inc'_`div'a2
local obs : display %12.0gc `n`div''
lincomest treat + treatL1_D  + treatL2_D  + treatL3_D 
mydisp
`append1' addrow("","`obs'") 
}
outreg, replay append(r)

outreg using "${est}other_reg/table_stacked_division_a`inc'", statfont( fs12 ) basefont(fs12) replay replace landscape




*******************  DUBE CONT ************************
outreg, clear
/*Short Run*/
estimates use ${est}ster/other/prominent_stacked_es_`inc'_allb
lincomest treat
mydisp
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend ct("","(`div')"   )

foreach div of local divisions {
estimates use ${est}ster/other/prominent_stacked_es_`inc'_`div'b
lincomest treat
mydisp
`append1'  ct("","(`div')"   )
}
outreg, replay append(r)


/*Long Run*/
estimates use ${est}ster/other/prominent_stacked_es_`inc'_allb2
local obs : display %12.0gc `n'
lincomest treat + treatL1_D  + treatL2_D  + treatL3_D 
mydisp
outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend  addrow("","`obs'")   

foreach div of local divisions {
estimates use ${est}ster/other/prominent_stacked_es_`inc'_`div'b2
local obs : display %12.0gc `n`div''
lincomest treat + treatL1_D  + treatL2_D  + treatL3_D 
mydisp
`append1' addrow("","`obs'") 
}
outreg, replay append(r)

outreg using "${est}other_reg/table_stacked_division_b`inc'", statfont( fs12 ) basefont(fs12) replay replace landscape

}




/*============= Overall ATT ==============*/
local append1 outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(4) noau   nolegend
outreg, clear

/* Dube Cont*/
foreach inc of numlist 75 100 50 {
	use ${data1}state_panel_mw_poverty_84, clear
	keep if year<2014
	
	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else  {
	createstack `inc' 25 10
	}

	qui sum N
	local n = r(N) * r(mean)
	
	estimates use ${est}ster/other/prominent_stacked_es_`inc'_allb
	lincomest treat
	mydisp
	if `inc'==75 outreg, store(r)  starloc(1) replace var stats(b se )  blankrow starlevels(10 5 1) bdec(4) noau nolegend ct("","(`div')"   ) addrow("","`n'") 
	else `append1' addrow("","`n'") 
}


/* Our Cont*/
foreach inc of numlist 75 100 50 {
	use ${data1}state_panel_mw_poverty_84, clear
	keep if year<2014
	
	if `inc'>50 {
		createstack `inc' 50 10
	} 
	else  {
	createstack `inc' 25 10
	}

	qui sum N
	local n = r(N) * r(mean)

	estimates use ${est}ster/other/prominent_stacked_es_`inc'_alla
	lincomest treat
	mydisp
	`append1' addrow("","`n'") 
}


outreg, replay append(r)

outreg using "${est}other_reg/table_stacked_overall_8413", statfont( fs12 ) basefont(fs12) replay replace landscape

